package com.wsc.lambda_expression;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class LambdaDemo {
    public static void main(String[] args) {
        LambdaDemo lambdaDemo = new LambdaDemo();
        /*
        //重写内部接口成为匿名方法
        MathOperation addition = (int a,int b) -> a + b;

        MathOperation subtraction = (a,b) -> a - b;

        MathOperation multiplication = (a,b) -> {return a * b;};

        MathOperation division = (a,b) -> a/b;

        SaySome sayHello = (msg -> System.out.println("hello"+msg));

        SaySome sayBey = msg -> System.out.println("bey"+msg);

        System.out.println(lambdaDemo.operate(5, 5, addition));

        sayHello.say("lambda");*/

        // ------------------------------- Lambda 只能引用有 final 修饰的外部局部变量,前面不加 final 表示隐式的加了 final 关键字 ---------------------------
       /*
        int i = 5;
        Converter<Integer,String> converter = param -> System.out.println(String.valueOf(param+i));
        converter.convert(3);
        */

        List<Integer> lists = Arrays.asList(1,2,3,4,5,6,7,8,9);

        MathOperation c = (a,b) -> a+b;

        System.out.println("输出所有数据");
        eval(lists, n -> true);
        System.out.println("输出偶数");
        eval(lists, n -> n%2 ==0);
        System.out.println("输出大于3的数");
        eval(lists, n -> n >3);

    }
    //定义内部接口以便于 Lambda 表达式实现
    interface MathOperation {
        int operation (int a,int b);
    }

    //定义内部接口以便于 Lambda 表达式实现
    interface SaySome{
        void say(String msg);
    }

    // 定义方法
    private int operate(int a,int b,MathOperation operation){
        return operation.operation(a, b);
    }

    // 定义内部接口以便于 Lambda 表达式实现
    interface Converter<T1,T2>{
        void convert(int i);
    }

    public static void eval(List<Integer> list , Predicate<Integer> predicate){
        for (Integer n : list){
            if(predicate.test(n)){
                System.out.println(n+" ");
            }
        }
    }
}
